/**
 * Copyright 2010-present Facebook.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.twodstar.photobattle;

import android.app.Application;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.facebook.model.GraphUser;
import com.parse.Parse;
import com.parse.ParseFacebookUtils;
import com.parse.ParseUser;

import java.util.List;

/**
 * Use a custom Application class to pass state data between Activities.
 */
public class PhotoBattleApplication extends Application 
{

	private String m_FBID;
    private List<GraphUser> selectedUsers;
    
    private String[] m_FrdUIDArray;
    private boolean m_IsAlreadyInitFrdUIDs = false;
    private static PhotoBattleApplication m_Inst = null;
    
    public PhotoBattleApplication() 
    {
		// TODO Auto-generated constructor stub
    	m_Inst = this;
	}
    
    public static PhotoBattleApplication Instance()
    {
    	return m_Inst;
    }

    public List<GraphUser> getSelectedUsers() 
    {
        return selectedUsers;
    }

    public void setSelectedUsers(List<GraphUser> users) 
    {
        selectedUsers = users;
    }
    
   
    
   
    public void SetAppFriendList(String[] pStrArray)
    {
    	m_FrdUIDArray = new String[pStrArray.length];
    	System.arraycopy(pStrArray, 0, m_FrdUIDArray, 0, pStrArray.length);
    	m_IsAlreadyInitFrdUIDs = true;
    }
    
    public boolean GetIsAlreadyInitFrdUIDs()
    {
    	return m_IsAlreadyInitFrdUIDs;
    }
    
    public String[] GetAppFrdList()
    {
    	return m_FrdUIDArray;
    }

    public void SetFBID(String FBID)
    {
    	m_FBID = FBID;
    }
    
    public String GetFBID()
    {
    	return m_FBID;
    }
   
    
    @Override
	public void onCreate() 
    {
		super.onCreate();

		Parse.initialize(this, "2n45sPsWC0u2VxEOH8OLKLP9TItQP4q8ip6xvBrg", "8RTFYCE4AHKo9TWrUYiKiJhFnCijINPIHy2Zmw0q");
		
		// Set your Facebook App Id in strings.xml
		ParseFacebookUtils.initialize(getString(R.string.app_id));

	}
    
    public String GetPlayerTitle()
	{
    	ParseUser PU = ParseUser.getCurrentUser();
    	if(PU != null)
    	{
    		int Level = PU.getInt("LevelNum");
    		
    		if(Level == 0)	// level 0 is just indicating that he hasn't create his first battle yet 
    			Level = 1;
    		
    		Level--; // java start form 0
    		String[] Titles = getResources().getStringArray(R.array.Titles);
    		
    		if(Level < Titles.length)
    		{
    			return Titles[Level];
    		}
    		else if(Level > Titles.length)
    		{
    			return Titles[Titles.length - 1];
    		}
    		
    		if(Titles != null && Titles.length > 0)
    			return Titles[0];
    		
    	}
    	
    	return "";
	}
    
    protected boolean isOnline() 
    {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected()) 
        {
            return true;
        } 
        else 
        {
            return false;
        }
    }
    
}
